package org.anderes.osgi.sample.ds.test;

import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.service.log.LogService;


// referenced in component.xml
public class ServiceComponent implements CommandProvider {
	
	private DictionaryService dictionary;
	private LogService logService;
	
	public void _dict(CommandInterpreter ci) {
		logService.log(LogService.LOG_INFO, "Command: dict");
		String arg = ci.nextArgument();
		String word = ci.nextArgument();
		if(arg == null) {
			return;
		}
		if (arg.equalsIgnoreCase("check")) {
			if(word != null && dictionary.check(word)) {
				ci.println(word + ": exists in the dictionary");
			} else {
				ci.println(word + ": doesn't exist in the dictionary");
			}
		}
		if (arg.equalsIgnoreCase("languages")) {
			String[] langs = dictionary.getLanguages();
			for(int i = 0; i < langs.length; i++) {
				ci.println("Languages available:");
				ci.println("\t " + langs[i]);
			}
		}
	}

	public String getHelp() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("---Generated by PDE---\n");
		buffer.append("\tdict check <word> - check for the existence of a word\n");
		buffer.append("\tdict languages - list the languages available\n");
		return buffer.toString();
	}
	
	public void setDictionary(DictionaryService d) {
		System.out.println("setDictionary");
		dictionary = d;
	}
	
	public void unsetDictionary(DictionaryService d) {
		dictionary = null;
	}
	
	public void setLogService(LogService logService) {
		System.out.println("setLogService");
		this.logService = logService;
	}
	
	public void unsetLogService(LogService logService) {
		this.logService = null;
	}
}